package top.chaileilei.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * @Description
 * @Time 2019/2/11
 * @Author CHAILEILEI
 * @Weblog www.chaileilei.top
 */
public class CuratorDemo {

    private static CuratorFramework client = CuratorFrameworkFactory.newClient("119.27.189.174:2181", 5000,
            3000, new ExponentialBackoffRetry(1000, 3));

    private static Stat stat = new Stat();
    public static void create() {
        //重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("119.27.189.174:2181", 5000,
                3000, retryPolicy);
        client.start();
    }

    /**
     * @Description 链式创建
     * @Param
     * @Return void
     * @Author CHAILEILEI
     * @Time 2019/2/12
     * @Blogs www.chaileilei.top
     */
    public static void createFluent(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client =
                CuratorFrameworkFactory.builder()
                        .connectString("119.27.189.174:2181")
                        .sessionTimeoutMs(5000)
                        .retryPolicy(retryPolicy)
                        .build();
        client.start();
    }
/**
 * @Description 命名空间，数据节点的任何操作都是基于该相对目录进行的
 * @Param
 * @Return void
 * @Author CHAILEILEI
 * @Time 2019/2/12
 * @Blogs www.chaileilei.top
 */
    public static void createFluentWithName(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client =
                CuratorFrameworkFactory.builder()
                        .connectString("119.27.189.174:2181")
                        .sessionTimeoutMs(5000)
                        .namespace("dgffhgh")
                        .retryPolicy(retryPolicy)
                        .build();
        client.start();
    }

    public static void main(String[] args) throws Exception {
        deleteNode();
    }

    /**
     * @Description 使用Curator创建节点
     * @Param
     * @Return void
     * @Author CHAILEILEI
     * @Time 2019/2/12
     * @Blogs www.chaileilei.top
     */
    public static void createNode() throws Exception {
        String path = "/fff";
        client.start();
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path,"init".getBytes());
    }

    /**
     * @Description 读取数据、修改数据和删除节点
     * @Param
     * @Return void
     * @Author CHAILEILEI
     * @Time 2019/2/12
     * @Blogs www.chaileilei.top
     */
    public static void deleteNode() throws Exception {
        String path = "/fff/sss";
        client.start();
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path,"haha".getBytes());
        //读取数据
        byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
        String string = new String(bytes);
        System.out.println(string);
        //更新数据
        System.out.println("Success set node for : " + path);
        client.setData().withVersion(stat.getVersion()).forPath(path,"sdgffg".getBytes());
        byte[] bytes2 = client.getData().storingStatIn(stat).forPath(path);
        String string2 = new String(bytes2);
        System.out.println(string2);

        client.delete().guaranteed()
                .deletingChildrenIfNeeded()
                .withVersion(stat.getVersion())
                .forPath(path);

    }
}











